1

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

public static class TokenizerMapper
extends Mapper<Object, Text,
Text, IntWritable> {

    public void map(
    Object key,
    Text value,
    Context context)

    throws IOException,
    InterruptedException {

        String line = value.toString();

        String parts[] =line.split(",");

        String user = parts[1];

        String login =
        parts[5];

        String logout =
        parts[7];

        int loginHour =
        Integer.parseInt(
        login.split(" ")[1]
        .split(":")[0]);

        int logoutHour =
        Integer.parseInt(
        logout.split(" ")[1]
        .split(":")[0]);

        int duration =
        logoutHour - loginHour;

        context.write(
        new Text(user),
        new IntWritable(duration));
    }
}
    

public static class IntSumReducer
extends Reducer<Text,
IntWritable,
Text,
IntWritable> {

   int first = 0, second = 0, third = 0;

    String firstKey = "", secondKey = "", thirdKey = "";    

    public void reduce(
    Text key,
    Iterable<IntWritable> values,
    Context context)

    throws IOException,
    InterruptedException {
    
     int sum = 0;

        for (IntWritable val : values) {

            sum += val.get();
        }

        if(sum > first) {

            third = second;
            thirdKey = secondKey;

            second = first;
            secondKey = firstKey;

            first = sum;
            firstKey = key.toString();
        }

        else if(sum > second) {

            third = second;
            thirdKey = secondKey;

            second = sum;
            secondKey = key.toString();
        }

        else if(sum > third) {

            third = sum;
            thirdKey = key.toString();
        }
    }

    protected void cleanup(Context context)
    throws IOException, InterruptedException {

        context.write(
        new Text(firstKey),
        new IntWritable(first));

        context.write(
        new Text(secondKey),
        new IntWritable(second));

        context.write(
        new Text(thirdKey),
        new IntWritable(third));
    }

}

    public static void main(String[] args)
    throws Exception {

        Configuration conf =
        new Configuration();

        String[] otherArgs =
        new GenericOptionsParser(conf, args)
        .getRemainingArgs();

        if (otherArgs.length != 2) {

            System.err.println(
            "Usage: wordcount <in> <out>");

            System.exit(2);
        }

        Job job =
        new Job(conf, "word count");

        job.setJarByClass(
        WordCount.class);

        job.setMapperClass(
        TokenizerMapper.class);

        job.setCombinerClass(
        IntSumReducer.class);

        job.setReducerClass(
        IntSumReducer.class);

        job.setOutputKeyClass(
        Text.class);

        job.setOutputValueClass(
        IntWritable.class);

        FileInputFormat.addInputPath(
        job,
        new Path(otherArgs[0]));

        FileOutputFormat.setOutputPath(
        job,
        new Path(otherArgs[1]));

        System.exit(
        job.waitForCompletion(true)
        ? 0 : 1);
    }
}





2

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

    public static class TokenizerMapper
    extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one =
        new IntWritable(1);

        private Text word = new Text();

        public void map(
        Object key,
        Text value,
        Context context)

        throws IOException, InterruptedException {
        

            StringTokenizer itr =
            new StringTokenizer(value.toString());

            while (itr.hasMoreTokens()) {

                word.set(itr.nextToken());

                context.write(word, one);
            }
        }
    }

    public static class IntSumReducer
    extends Reducer<Text, IntWritable,
    Text, IntWritable> {

        private IntWritable result =
        new IntWritable();

        public void reduce(
        Text key,
        Iterable<IntWritable> values,
        Context context)

        throws IOException, InterruptedException {

            int sum = 0;

            for (IntWritable val : values) {

                sum += val.get();
            }

            result.set(sum);

            context.write(key, result);
        }
        
    }

    public static void main(String[] args)
    throws Exception {

        Configuration conf =
        new Configuration();

        String[] otherArgs =
        new GenericOptionsParser(conf, args)
        .getRemainingArgs();

        if (otherArgs.length != 2) {

            System.err.println(
            "Usage: wordcount <in> <out>");

            System.exit(2);
        }

        Job job =
        new Job(conf, "word count");

        job.setJarByClass(
        WordCount.class);

        job.setMapperClass(
        TokenizerMapper.class);

        job.setCombinerClass(
        IntSumReducer.class);

        job.setReducerClass(
        IntSumReducer.class);

        job.setOutputKeyClass(
        Text.class);

        job.setOutputValueClass(
        IntWritable.class);

        FileInputFormat.addInputPath(
        job,
        new Path(otherArgs[0]));

        FileOutputFormat.setOutputPath(
        job,
        new Path(otherArgs[1]));

        System.exit(
        job.waitForCompletion(true)
        ? 0 : 1);
    }
}


3

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

    public static class TokenizerMapper
    extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one =
        new IntWritable(1);

        private Text word = new Text();

				public void map(
				Object key,
				Text value,
				Context context)
				
				throws IOException, InterruptedException {
				
				    String line = value.toString();
				
				    if(line.contains("UserId")) {
				        return;
				    }
				
				    String parts[] = line.split(",");
				
				    String userId = parts[0];
				
				    String trackId = parts[1];
				
				    int shared =
				    Integer.parseInt(parts[2]);
				
				    context.write(
				    new Text(trackId + " Listener"),
				    new IntWritable(1));
				
				    context.write(
				    new Text(trackId + " Shared"),
				    new IntWritable(shared));
	}
    }

    public static class IntSumReducer
    extends Reducer<Text, IntWritable,
    Text, IntWritable> {

        private IntWritable result =
        new IntWritable();

public void reduce(
Text key,
Iterable<IntWritable> values,
Context context)

throws IOException, InterruptedException {

    int sum = 0;

    for (IntWritable val : values) {

        sum += val.get();
    }

    result.set(sum);

    context.write(key, result);
}
}

    public static void main(String[] args)
    throws Exception {

        Configuration conf =
        new Configuration();

        String[] otherArgs =
        new GenericOptionsParser(conf, args)
        .getRemainingArgs();

        if (otherArgs.length != 2) {

            System.err.println(
            "Usage: wordcount <in> <out>");

            System.exit(2);
        }

        Job job =
        new Job(conf, "word count");

        job.setJarByClass(
        WordCount.class);

        job.setMapperClass(
        TokenizerMapper.class);

        job.setCombinerClass(
        IntSumReducer.class);

        job.setReducerClass(
        IntSumReducer.class);

        job.setOutputKeyClass(
        Text.class);

        job.setOutputValueClass(
        IntWritable.class);

        FileInputFormat.addInputPath(
        job,
        new Path(otherArgs[0]));

        FileOutputFormat.setOutputPath(
        job,
        new Path(otherArgs[1]));

        System.exit(
        job.waitForCompletion(true)
        ? 0 : 1);
    }
}


4

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

    public static class TokenizerMapper
    extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one =
        new IntWritable(1);

        private Text word = new Text();

        public void map(
						Object key,
						Text value,
						Context context)
						
						throws IOException, InterruptedException {
						
						    String line = value.toString();
						
						    if(line.contains("UserId")) {
						        return;
						    }
						
						    String parts[] = line.split(",");
						
						    String trackId = parts[1];
						
						    int radio =
						    Integer.parseInt(parts[3]);
						
						    int skip =
						    Integer.parseInt(parts[4]);
						
						    context.write(
						    new Text(trackId + " Radio"),
						    new IntWritable(radio));
						
						    context.write(
				    new Text(trackId + " Skip"),
			    new IntWritable(skip));
	}
    }

    public static class IntSumReducer
    extends Reducer<Text, IntWritable,
    Text, IntWritable> {

        private IntWritable result =
        new IntWritable();

					public void reduce(
					Text key,
					Iterable<IntWritable> values,
					Context context)
					
					throws IOException, InterruptedException {
					
					    int sum = 0;
					
					    for (IntWritable val : values) {
					
					        sum += val.get();
					    }
					
					    result.set(sum);
					
					    context.write(key, result);
					}
		}

    public static void main(String[] args)
    throws Exception {

        Configuration conf =
        new Configuration();

        String[] otherArgs =
        new GenericOptionsParser(conf, args)
        .getRemainingArgs();

        if (otherArgs.length != 2) {

            System.err.println(
            "Usage: wordcount <in> <out>");

            System.exit(2);
        }

        Job job =
        new Job(conf, "word count");

        job.setJarByClass(
        WordCount.class);

        job.setMapperClass(
        TokenizerMapper.class);

        job.setCombinerClass(
        IntSumReducer.class);

        job.setReducerClass(
        IntSumReducer.class);

        job.setOutputKeyClass(
        Text.class);

        job.setOutputValueClass(
        IntWritable.class);

        FileInputFormat.addInputPath(
        job,
        new Path(otherArgs[0]));

        FileOutputFormat.setOutputPath(
        job,
        new Path(otherArgs[1]));

        System.exit(
        job.waitForCompletion(true)
        ? 0 : 1);
    }
}


5

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

    public static class TokenizerMapper
    extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one =
        new IntWritable(1);

        private Text word = new Text();

					public void map(
					Object key,
					Text value,
					Context context)
					
					throws IOException, InterruptedException {
					
					    String line = value.toString();
					
					    if(line.contains("userId")) {
					        return;
					    }
					
					    String parts[] = line.split(",");
					
					    String movieId = parts[1];
					
					    int rating =
					    (int) Float.parseFloat(parts[2]);
					
					    word.set(movieId);
					
					    context.write(word,
					    new IntWritable(rating));
					}
    }

public static class IntSumReducer
extends Reducer<Text,IntWritable,
Text,IntWritable> {

    private IntWritable result =
    new IntWritable();

    int maxRating = 0;

   public void reduce(
Text key,
Iterable<IntWritable> values,
Context context)

throws IOException, InterruptedException {

    int sum = 0;

    int count = 0;

    for (IntWritable val : values) {

        sum += val.get();

        count++;
    }

    int avg = sum / count;

    if(avg >= 4) {

        result.set(avg);

        context.write(key, result);
    }
}
}

    public static void main(String[] args)
    throws Exception {

        Configuration conf =
        new Configuration();

        String[] otherArgs =
        new GenericOptionsParser(conf, args)
        .getRemainingArgs();

        if (otherArgs.length != 2) {

            System.err.println(
            "Usage: wordcount <in> <out>");

            System.exit(2);
        }

        Job job =
        new Job(conf, "word count");

        job.setJarByClass(
        WordCount.class);

        job.setMapperClass(
        TokenizerMapper.class);

        job.setCombinerClass(
        IntSumReducer.class);

        job.setReducerClass(
        IntSumReducer.class);

        job.setOutputKeyClass(
        Text.class);

        job.setOutputValueClass(
        IntWritable.class);

        FileInputFormat.addInputPath(
        job,
        new Path(otherArgs[0]));

        FileOutputFormat.setOutputPath(
        job,
        new Path(otherArgs[1]));

        System.exit(
        job.waitForCompletion(true)
        ? 0 : 1);
    }
}